package com.bionic.banking.dao.classes;

import com.bionic.banking.core.entity.User;
import com.bionic.banking.dao.IUserDao;
import com.bionic.banking.dao.connection.DBQueryManager;
import java.sql.*;
import java.util.List;

public class UserDaoImpl implements IUserDao {
    
    protected final String SQL_SELECT = "SELECT * FROM " + getTableName();
    protected final String SQL_SELECT_VERIFY = "SELECT * FROM " + getTableName()
            + " WHERE LOGIN=?, PASSWORD=?";
    protected final String SQL_INSERT = "INSERT INTO " + getTableName() + "(LOGIN, PASSWORD) VALUES(?,?)";
    protected final String SQL_UPDATE = "UPDATE " + getTableName() + " SET LOGIN=?, PASSWORD=? ";
    protected final String SQL_DELETE = "DELETE FROM " + getTableName();
    protected final String SQL_WHERE = " WHERE " + getKeyColumn() + "=?";
    protected static final int COLUMN_USERID = 1;
    protected static final int COLUMN_LOGIN = 2;
    protected static final int COLUMN_PASSWORD = 3;
    protected int maxRows;

    @Override
    public String getTableName() {
        return "USER";
    }

    @Override
    public String getKeyColumn() {
        return "USER_ID";
    }

    @Override
    public DBQueryManager<User> getDBManager() {
        return new DBQueryManager<>(this);
    }

    @Override
    public User populateDto(ResultSet rs) throws SQLException {
        User dto = new User();
        dto.setUserId(rs.getInt(COLUMN_USERID));
        dto.setLogin(rs.getString(COLUMN_LOGIN));
        dto.setPassword(rs.getString(COLUMN_PASSWORD));
        //dto.setSince(rs.getString(COLUMN_CREATEDON));
        return dto;
    }

    @Override
    public Integer create(User user) throws SQLException {
        Object[] params = new Object[]{
            user.getLogin(),
            user.getPassword()
        };
        Integer newUserId = getDBManager().executeCreateQuery(SQL_INSERT, params);
        user.setUserId(newUserId);//update with new generated PK in DB 
        return newUserId;
    }

    @Override
    public boolean update(User user) throws SQLException {
        Object[] params = new Object[]{
            user.getLogin(),
            user.getPassword(),
            user.getUserId()
        };
        final String query = SQL_UPDATE + SQL_WHERE;
        return getDBManager().executeSaveQuery(query, params);
    }

    @Override
    public void remove(int id) throws SQLException {
        final String query = SQL_DELETE + SQL_WHERE;
        getDBManager().executeRemoveQuery(query, id);
    }

    @Override
    public User findById(int userid) throws SQLException {
        String query = SQL_SELECT + SQL_WHERE;
        Object[] params = new Object[]{new Integer(userid)};
        return getDBManager().executeSelectSingleQuery(query, params);
    }

    @Override
    public User findByLogin(String login) throws SQLException {
        String query = SQL_SELECT + " WHERE LOGIN = ?";
        Object[] params = new Object[]{login};
        return getDBManager().executeSelectSingleQuery(query, params);
    }

    @Override
    public User findByEmail(String email) throws SQLException {
        String query = SQL_SELECT + " WHERE EMAIL = ?";
        Object[] params = new Object[]{email};
        return getDBManager().executeSelectSingleQuery(query, params);
    }

    @Override
    public boolean verify(String login, String password) throws SQLException {
        Object[] params = new Object[]{login, password};
        List<User> users = getDBManager().executeSelectQuery(SQL_SELECT_VERIFY, params);
        if (users == null || users.size() == 0) {
            return false;
        }
        return true;
        //or
        //User user = findByLogin(login);
        //return user.getPassword() == password; 
    }

    @Override
    public List<User> findAll() throws SQLException {
        String query = SQL_SELECT;
        return getDBManager().executeSelectQuery(query, null);    
    }

    @Override
    public void setMaxRows(int maxRows) {
        this.maxRows = maxRows;
    }

    @Override
    public int getMaxRows() {
        return maxRows;
    }
}